package com.zoro.interviewee.conc;

import java.util.concurrent.CountDownLatch;
import java.util.concurrent.TimeUnit;

/**
 * @author zhaoxingwu
 */
public class CountDownLatchTest {

    public static void main(String[] args) throws InterruptedException {
        final int times = 5;
        CountDownLatch cdl = new CountDownLatch(times);

        for (int i = 0; i < times *2; i++) {
            final int sleepSecond = i + 1;
            Thread t = new Thread(() -> {
                try {
                    System.out.printf(" 线程：%s 开始，%s 秒后等待 %n", Thread.currentThread().getName(), sleepSecond);
                    TimeUnit.SECONDS.sleep(sleepSecond);
                    System.out.printf(" 线程：%s 等待结束 %n", Thread.currentThread().getName());

                } catch (Exception e) {
                    e.printStackTrace();
                }finally {
                    cdl.countDown();
                    System.out.printf(" 线程：%s 工作结束 %n", Thread.currentThread().getName());
                }
            });
            t.setDaemon(true);
            t.start();
        }

        cdl.await();

    }
}
